package py.ucom.sistemas.web.entradaspy.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import py.ucom.sistemas.web.entradaspy.db.componenentes.Usuario;
import py.ucom.sistemas.web.entradaspy.db.consultas.Login;

public class CheckLogin extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession sesion = req.getSession();
		PrintWriter out = resp.getWriter();
		String email = req.getParameter("user_email");
		String password = req.getParameter("user_password");
		Login conn = new Login();
		Usuario u = conn.checkUser(email);
		if(u.id_usuario != null){
			if(conn.checkPass(u.id_usuario,password)){
				sesion.setAttribute("session",u.id_usuario);
				sesion.setAttribute("usuarioid",u);
				resp.sendRedirect("index.jsp");
			}
			else{
				resp.sendRedirect("error.jsp");
			}
		}
		else{
			/*out.println("Fallo de login. Usuario o password erroneos");
			out.println("<a href = login.jsp>Login</a>");
			out.println("<a href = register.jsp>Registrarse</a>");*/
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	

}
